<template>
  <el-container class="home_container">
    <!-- 头部区域 -->
    <el-header>
      <div class="logo">
        <img src="@/assets/logo.png" alt="">
        <span>电商后台管理系统</span>
      </div>
      <el-button type="info" @click="logout">退出</el-button>
    </el-header>
    <!-- 主体区域 -->
    <el-container>
      <!-- 侧边栏 -->
      <el-aside :width="isCollapse ? '64px' : '200px'">
        <!-- 折叠与展开按钮 -->
        <div class="toggle-button" @click="toggleCollapse">|||</div>
        <!-- 侧边栏菜单区域 -->
        <el-menu 
        background-color="#333744" 
        text-color="#fff" 
        active-text-color="#409eff"
        :unique-opened="true" 
        :collapse="isCollapse"
        :collapse-transition="false"
        :router="true"
        :default-active="activepath">
          <!-- 一级菜单 -->
          <el-submenu :index="`${index + 1}`" v-for="(item, index) in menulist" :key="item.id">
            <!-- 一级菜单模板区域 -->
            <template slot="title">
              <!-- 图标 -->
              <i :class="icons[item.id]"></i>
              <!-- 文本 -->
              <span>{{ item.authName }}</span>
            </template>
            <!-- 二级菜单 -->
            <el-menu-item :index="'/home/'+subItem.path" v-for="subItem in item.children" :key="subItem.id" @click="saveNavState(subItem.path)">
              <!-- 二级菜单模板区域 -->
              <template slot="title">
                <!-- 图标 -->
                <i class="el-icon-menu"></i>
                <!-- 文本 -->
                <span>{{ subItem.authName }}</span>
              </template>
            </el-menu-item>
          </el-submenu>
        </el-menu>

      </el-aside>
      <!-- 右侧内容区域 -->
      <el-main>
        <!-- 路由占位符 -->
        <router-view></router-view>
      </el-main>
    </el-container>
  </el-container>
</template>

<script>
export default {
  name: 'Home',
  data() {
    return {
      // 左侧菜单数据
      menulist: [],
      // 一级菜单字体图标
      icons:{
        '125':'iconfont icon-user',
        '103':'iconfont icon-tijikongjian',
        '101':'iconfont icon-shangpin',
        '102':'iconfont icon-danju',
        '145':'iconfont icon-baobiao'
      },
      // 是否折叠
      isCollapse:false,
      //二级分类选中状态
      activepath:''
    }
  },
  mounted() {
    //当页面加载时，获取左侧菜单
    this.getMenuList()
    // 当页面加载时，赋值session中的 activepath 值
    this.activepath = window.sessionStorage.getItem('activepath')
  },
  methods: {
    logout() {
      // 清空 session 数据
      window.sessionStorage.clear()
      // 重定向到登陆页面
      this.$router.push('/login')
    },
    //获取左侧菜单
    async getMenuList() {
      const { data: res } = await this.$http.get('menus')
      // 请求失败，return 错误提示
      if (res.meta.status !== 200) return this.$message.error(res.meta.msg)
      // 请求成功，赋值 menulist
      this.menulist = res.data
    },
    //点击按钮切换菜单的折叠与展开
    toggleCollapse(){
      this.isCollapse = !this.isCollapse
    },
    // 点击二级菜单保存 path 值
    saveNavState(activepath){
      window.sessionStorage.setItem('activepath',activepath)
      // 当 session 中有 activepath 点击其他 路由链接后 ，在点回去时 不会处于选中状态，需要重新赋值
      this.activepath = activepath
    }

  }
}
</script>

<style lang="less" scoped>
.home_container {
  height: 100%;
}

.el-header {
  background-color: #373D41;
  display: flex;
  justify-content: space-between;
  align-items: center;
  padding-left: 0;
  color: #fff;
  font-size: 20px;

  .logo {
    display: flex;
    align-items: center;

    img {
      width: 60px;
      height: 60px;
    }

    span {
      margin-left: 15px;
    }
  }
}

.el-aside {
  background-color: #333744;
  .el-menu{
    border-right: none;
  }
}

.el-main {
  background-color: #eaedf1;
}
.iconfont{
  margin-right: 10px;
}
.toggle-button{
  background-color: #4A5064;
  font-size: 10px;
  line-height: 24px;
  color: #fff;
  text-align: center;
  letter-spacing: 0.2em;
  cursor: pointer;
}
</style>